<?php

/**
 * This is the model class for table "transporter".
 *
 * The followings are the available columns in table 'transporter':
 * @property integer $id_transporter
 * @property string $company_name
 * @property string $street
 * @property string $nr_street
 * @property string $zip_code
 * @property string $city
 * @property string $date_add
 * @property string $date_upd
 * @property integer $del_flg
 */
class Transporter extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Transporter the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'transporter';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
            array('company_name, zip_code', 'required'),
            array('company_name','unique', 'message'=>Yii::t('app', 'This company name already exists.')),
			array('del_flg', 'numerical', 'integerOnly'=>true),
			array('company_name, street, nr_street', 'length', 'max'=>128),
			array('zip_code', 'length', 'max'=>16),
			array('city', 'length', 'max'=>132),
			array('date_add, date_upd', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_transporter, company_name, street, nr_street, zip_code, city, date_add, date_upd, del_flg', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'trd' => array(self::HAS_MANY, 'TransporterDetail', 'id_transporter'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_transporter' => '#',
			'company_name' => Yii::t('app', 'Company Name'),
			'street' => Yii::t('app', 'Street'),
			'nr_street' => Yii::t('app', 'Nr Street'),
			'zip_code' => Yii::t('app', 'Zip Code'),
			'city' => Yii::t('app', 'City'),
			'date_add' => Yii::t('app', 'Date Add'),
			'date_upd' => Yii::t('app', 'Date Update'),
			'del_flg' => 'Del Flg',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_transporter',$this->id_transporter);
		$criteria->compare('company_name',$this->company_name,true);
		$criteria->compare('street',$this->street,true);
		$criteria->compare('nr_street',$this->nr_street,true);
		$criteria->compare('zip_code',$this->zip_code,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('date_add',$this->date_add,true);
		$criteria->compare('date_upd',$this->date_upd,true);
		$criteria->compare('del_flg',$this->del_flg);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    public function beforeSave() {
        $now = new CDbExpression('NOW()');
        if ($this->isNewRecord) $this->date_add = $now;

        $this->date_upd = $now;

        return parent::beforeSave();
    }

    /**
     * get all trensporter to array
     * @return array
     */
    public static function getListTransporter(){
        // get list Transport company to select
        $mCompanyTran = Transporter::model()->findAll('del_flg = 0');
        $listComTran = array();
        if ($mCompanyTran) {
            foreach ($mCompanyTran as $mct) {
                $listComTran[$mct->id_transporter] = $mct->company_name;
            }
        }

        return $listComTran;
    }

}